import {
	Entity,
	Column,
	PrimaryGeneratedColumn,
	CreateDateColumn,
	UpdateDateColumn,
	OneToMany,
} from "typeorm";
import { Order } from "../../order/entities/order.entity";
import { PointsRecord } from "../../points/entities/points-record.entity";
import { Address } from "../../address/entities/address.entity";
import { DateTransformer } from "../../../common/transformers/date.transformer";

@Entity("users")
export class User {
	@PrimaryGeneratedColumn()
	id: number;

	@Column({ length: 50, unique: true })
	username: string;

	@Column({ length: 100 })
	password: string;

	@Column({ length: 50, nullable: true })
	nickname: string;

	@Column({ length: 20, nullable: true })
	phone: string;

	@Column({ default: 0 })
	points: number;

	@Column({
		type: "enum",
		enum: ["正常", "禁用"],
		default: "正常",
	})
	status: string;

	@Column({
		type: "enum",
		enum: ["admin", "user"],
		default: "user",
	})
	role: string;

	@Column({ type: "text", nullable: true })
	remark: string;

	@CreateDateColumn({
		type: "datetime",
		name: "created_at",
		transformer: new DateTransformer(),
		nullable: false,
	})
	createdAt: Date;

	@UpdateDateColumn({
		type: "datetime",
		name: "updated_at",
		transformer: new DateTransformer(),
		nullable: false,
	})
	updatedAt: Date;

	// 关系
	@OneToMany(() => Order, (order) => order.user)
	orders: Order[];

	@OneToMany(() => PointsRecord, (pointsRecord) => pointsRecord.user)
	pointsRecords: PointsRecord[];

	@OneToMany(() => Address, (address) => address.user)
	addresses: Address[];
}
